package Sort;

public class QuickSort {

    public static void quick(int[] arr){
        recursion(arr,0, arr.length-1);
    }

    private static void recursion(int[] arr,int left,int right){
        if (left >= right){
            return;
        }
        int mid = doubleQuickSort(arr,left,right);
        recursion(arr,left,mid-1);
        recursion(arr,mid + 1,right);
    }

    //单边快速排序
    private static int singQuickSort(int[] arr,int left,int right){
        int temp = arr[right];
        int i = left;
        int j = left;
        while (j < right){
            if (arr[j] < temp){
                if (j != i){
                    swap(arr,i,j);
                }
                i++;
            }
            j++;
        }
        swap(arr,i,right);
        return i;
    }

    private static void swap(int[] arr,int i,int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    //双边快速排序
    private static int doubleQuickSort(int[] arr,int left,int right){
        int temp = arr[left];
        int i = left;
        int j = right;
        while (i < j){

            while (i < j && arr[j] > temp){
                j--;
            }
            while (i < j && arr[i] <= temp){
                i++;
            }

            swap(arr,i,j);
        }
        swap(arr,i,left);
        return i;
    }

}
